from Goldenberry.optimization.ga.GbCrossoverMgr.CrossoverStrategy import CrossoverStrategy
import random as ran
import numpy as np

class OnePointBinaryCrossover(CrossoverStrategy):

    def __init__(self):
        self.name='OnePointBinaryCrossover'

    def cross(self, parents):
     """Cross the parents using one point crossover.
    
     :param parents: array list of parents individual to cross, each parent is a GbIndividual
     :returns: two numpy array genotype1, genotype2 generated, corresponding to the genotype of the new individuals
     """
     pcut = int(ran.random()*len(parents[0].genotype))
     genotype1 = np.concatenate((parents[0].genotype[:pcut],parents[1].genotype[pcut:]))
     genotype2 = np.concatenate((parents[1].genotype[:pcut],parents[0].genotype[pcut:]))
     return genotype1, genotype2

class TwoPointBinaryCrossover(CrossoverStrategy):

    def __init__(self):
        self.name='TwoPointBinaryCrossover'

    def cross(self, parents):
     """Cross the parents using multiple point crossover.
    
     :param parents: array list of parents individual to cross, each parent is a GbIndividual
     :returns: two numpy array genotype1, genotype2 generated,  corresponding to the genotype of the new individuals
     """
     pts_cut = ran.randint(1,len(parents[0].genotype)-1), ran.randint(1,len(parents[0].genotype)-1)#np.random.random_integers(1,len(parents[0].genotype)-1, 2)
     pcut_one, pcut_two = np.amin(pts_cut), np.amax(pts_cut)
     genotype1 = np.concatenate((parents[0].genotype[:pcut_one],parents[1].genotype[pcut_one:pcut_two], parents[0].genotype[pcut_two:]))
     genotype2 = np.concatenate((parents[1].genotype[:pcut_one],parents[0].genotype[pcut_one:pcut_two], parents[1].genotype[pcut_two:]))
     return genotype1, genotype2
